﻿using System;
using System.Collections.Generic;
using System.Text;
using CodePlex.Resourceful.Microsoft.Azure;
using System.Text.RegularExpressions;
using System.IO;

namespace CodePlex.Resourceful.Examples
{
    public static class AzureTableExample
    {




        public static void Run()
        {

            string storage = Environment.GetEnvironmentVariable("AZURE");

            string accountName = storage.Split(':')[0];
            string base64key = storage.Split(':')[1];

          
            AzureTableClient client = new AzureTableClient(accountName, base64key);
            client.Timeout = TimeSpan.FromSeconds(20);
            client.Tracer.AddListener(Console.Out);




            //DeleteAllTables(client); return;
            
            string table = ("t" + Guid.NewGuid()).Replace("-", "");
           
            client.CreateTable(table);

            string partition = "partition1";

            SimpleExample(client, table, partition);
            BatchExample(client, table, partition,10);

            client.DeleteTable(table);


        }






        private static void DeleteAllTables(AzureTableClient client)
        {
            foreach (Match m in Regex.Matches(client.QueryTables().AsString(),
                @"<d\:TableName>(.*?)<\/d\:TableName>", RegexOptions.None))
                client.DeleteTable(m.Groups[1].Value);
        }



        private static void SimpleExample(AzureTableClient client, string table, string partition)
        {
            string row1 = "row-" + Guid.NewGuid();

            client.InsertEntity(table, partition, row1,
                AzureProperty.String("Address", "Mountain View"),
                AzureProperty.Binary("BinaryData", new byte[] { 0x01, 0x02 }),
                AzureProperty.Boolean("IsActive", true),
                AzureProperty.DateTime("CustomerSince", DateTime.Parse("2008-07-10")),
                AzureProperty.Double("AmountDue", 200.23),
                AzureProperty.Guid("CustomerCode", Guid.NewGuid()),
                AzureProperty.Int32("Age", 23),
                AzureProperty.Int64("NumOfOrders", 255));


            client.ExecuteBatch(client.GetEntityRequest(table, partition, row1));
            client.ExecuteBatch(client.QueryEntitiesRequest(table, "(AmountDue eq 200.23)",1));

            client.UpdateEntityRequest(table, partition, row1,
                AzureProperty.String("Address", null),
                AzureProperty.Binary("BinaryData", null),
                AzureProperty.Boolean("IsActive", null),
                AzureProperty.DateTime("CustomerSince", null),
                AzureProperty.Double("AmountDue", null),
                AzureProperty.Guid("CustomerCode", null),
                AzureProperty.Int32("Age", null),
                AzureProperty.Int64("NumOfOrders", null));


            client.MergeEntity(table, partition, row1, AzureProperty.Double("AmountDue", 200.24));

            client.GetEntity(table, partition, row1);
            client.QueryEntities(table, "(AmountDue eq 200.24)",1);
            client.DeleteEntity(table, partition, row1);
        }



        private static void BatchExample(AzureTableClient client, string table, string partition, int rowsInBatch)
        {
            List<Guid> rows = new List<Guid>();
            for (int i = 0; i < rowsInBatch; i++)
            {
                rows.Add(Guid.NewGuid());
            }

            List<AzureTableClient.BatchableRequest> calls = new List<AzureTableClient.BatchableRequest>();

            client.ExecuteBatch(Fn.Select(rows, delegate(Guid item)
            {
                string row = "row" + item;

                return client.InsertEntityRequest(table, partition, row,
                AzureProperty.String("Address", "Mountain View"),
                AzureProperty.Binary("BinaryData", new byte[] { 0x01, 0x02 }),
                AzureProperty.Boolean("IsActive", true),
                AzureProperty.DateTime("CustomerSince", DateTime.Parse("2008-07-10")),
                AzureProperty.Double("AmountDue", 200.23),
                AzureProperty.Guid("CustomerCode", Guid.NewGuid()),
                AzureProperty.Int32("Age", 23),
                AzureProperty.Int64("NumOfOrders", 255));

            }));

            client.ExecuteBatch(Fn.Select(rows, delegate(Guid item)
            {
                string row = "row" + item;

                return client.UpdateEntityRequest(table, partition, row,
                    AzureProperty.String("Address", null),
                    AzureProperty.Binary("BinaryData", null),
                    AzureProperty.Boolean("IsActive", null),
                    AzureProperty.DateTime("CustomerSince", null),
                    AzureProperty.Double("AmountDue", null),
                    AzureProperty.Guid("CustomerCode", null),
                    AzureProperty.Int32("Age", null),
                    AzureProperty.Int64("NumOfOrders", null));

            }));

            client.ExecuteBatch(Fn.Select(rows, delegate(Guid item)
            {
                string row = "row" + item;

                return client.MergeEntityRequest(table, partition, row, AzureProperty.Double("AmountDue", 200.24));

            }));

            client.ExecuteBatch(Fn.Select(rows, delegate(Guid item)
            {
                string row = "row" + item;

                return client.DeleteEntityRequest(table, partition, row);

            }));

        }


    }
}
